﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.Revit;

namespace HYCableTrayConnection
{
	public class Common
	{
		public static bool NewElbowFittingByRevitCommand(Document doc, HYCableTray<CableTray> ct1, Connector connector1, Connector connector2)
		{
			bool result = false;
			CableTrayFitingSel.GetCableTrayFittingType(doc, CableTrayFittingType.HorizontalElbow, ct1);
			if (doc.Create.NewElbowFitting(connector1, connector2) != null)
			{
				result = true;
			}
			return result;
		}

		public static bool NewTeeFittingByRevitCommand(Document doc, XYZ intersectionPoint, HYCableTray<CableTray> myCustomCableTray, ref HYCableTray<CableTray> myCustomCableTray1, HYCableTray<CableTray> myCustomCableTray2, ref HYCableTray<CableTray> myCustomCableTray3)
		{
			bool result = false;
			myCustomCableTray3 = Common.SeparateCableTray(ref myCustomCableTray1, myCustomCableTray2, intersectionPoint);
			if (myCustomCableTray3 == null)
			{
				throw new MyException("请检查桥架位置");
			}
			Connector closeConnector = myCustomCableTray.GetCloseConnector(intersectionPoint);
			Connector closeConnector2 = myCustomCableTray1.GetCloseConnector(intersectionPoint);
			Connector closeConnector3 = myCustomCableTray3.GetCloseConnector(intersectionPoint);
			if (!Common.IsMeetVertical(closeConnector, closeConnector3))
			{
				throw new MyException("请检查桥架位置");
			}
			if (closeConnector == null || closeConnector2 == null || closeConnector3 == null || closeConnector.IsConnected || closeConnector2.IsConnected || closeConnector3.IsConnected)
			{
				throw new MyException("桥架已经连接");
			}
			CableTrayFitingSel.GetCableTrayFittingType(doc, CableTrayFittingType.HorizontalTee, myCustomCableTray);
			if (doc.Create.NewTeeFitting(closeConnector2, closeConnector3, closeConnector) != null)
			{
				result = true;
			}
			return result;
		}

		public static bool NewCrossFittingByRevitCommand(Document doc, ref HYCableTray<CableTray> myCustomCableTray, HYCableTray<CableTray> myCustomCableTray1, ref HYCableTray<CableTray> myCustomCableTray2, HYCableTray<CableTray> myCustomCableTray3, ref HYCableTray<CableTray> myCustomCableTray4, ref HYCableTray<CableTray> myCustomCableTray5)
		{
			bool result = false;
			XYZ xyz = myCustomCableTray.IntersectionPoint(myCustomCableTray2.CableTray);
			if (xyz == null)
			{
				throw new MyException("请检查桥架位置");
			}
			myCustomCableTray4 = Common.SeparateCableTray(ref myCustomCableTray, myCustomCableTray1, xyz);
			myCustomCableTray5 = Common.SeparateCableTray(ref myCustomCableTray2, myCustomCableTray3, xyz);
			Connector closeConnector = myCustomCableTray.GetCloseConnector(xyz);
			Connector closeConnector2 = myCustomCableTray4.GetCloseConnector(xyz);
			Connector closeConnector3 = myCustomCableTray2.GetCloseConnector(xyz);
			Connector closeConnector4 = myCustomCableTray5.GetCloseConnector(xyz);
			if (!Common.IsMeetVertical(closeConnector, closeConnector3))
			{
				throw new MyException("请检查桥架位置");
			}
			if (closeConnector == null && closeConnector.IsConnected && closeConnector2 == null && closeConnector2.IsConnected && closeConnector3 == null && closeConnector3.IsConnected && closeConnector4 == null && closeConnector4.IsConnected)
			{
				throw new MyException("桥架已经连接");
			}
			CableTrayFitingSel.GetCableTrayFittingType(doc, CableTrayFittingType.HorizontalCross, myCustomCableTray);
			if (doc.Create.NewCrossFitting(closeConnector, closeConnector2, closeConnector3, closeConnector4) != null)
			{
				result = true;
			}
			return result;
		}

		public static void RefreshModel(Document doc, FamilyInstance fitting)
		{
			XYZ point = (fitting.Location as LocationPoint).Point;
			XYZ xyz = point - new XYZ(point.X + 0.001, point.Y, point.Z);
			ElementTransformUtils.MoveElement(doc, fitting.Id, xyz);
			doc.Regenerate();
			ElementTransformUtils.MoveElement(doc, fitting.Id, -xyz);
			doc.Regenerate();
		}

		public static bool IsVerticalIntersection(HYCableTray<CableTray> cableTray, HYCableTray<CableTray> cableTrayOther)
		{
			bool result = false;
			Curve locationCurve = cableTray.GetLocationCurve();
			Curve locationCurve2 = cableTrayOther.GetLocationCurve();
			XYZ xyz = locationCurve.GetEndPoint(1) - locationCurve.GetEndPoint(0);
			XYZ xyz2 = locationCurve2.GetEndPoint(1) - locationCurve2.GetEndPoint(0);
			if (Math.Abs(xyz.AngleTo(xyz2) - Math.PI*.5) < 0.001)
			{
				result = true;
			}
			return result;
		}

		public static HYCableTray<CableTray>[] GetTrayWidthSort(params HYCableTray<CableTray>[] cts)
		{
			for (int i = 0; i < cts.Length - 1; i++)
			{
				for (int j = 0; j < cts.Length - 1 - i; j++)
				{
					if (cts[j].CableTray.Width > cts[j + 1].CableTray.Width)
					{
						HYCableTray<CableTray> hycableTray = cts[j];
						cts[j] = cts[j + 1];
						cts[j + 1] = hycableTray;
					}
				}
			}
			return cts;
		}

		public static HYCableTray<CableTray> SeparateCableTray(ref HYCableTray<CableTray> myCustomCableTray1, HYCableTray<CableTray> myCustomCableTray2, XYZ intersectionPoint)
		{
			HYCableTray<CableTray> result = null;
			if (myCustomCableTray1.CableTray.UniqueId == myCustomCableTray2.CableTray.UniqueId)
			{
				IList<CableTray> list = new List<CableTray>();
				list = myCustomCableTray1.Separate(intersectionPoint);
				myCustomCableTray1 = new HYCableTray<CableTray>(list[0]);
				result = new HYCableTray<CableTray>(list[1]);
			}
			else if (myCustomCableTray1.IsCableTrayOnLine(myCustomCableTray2.CableTray) || myCustomCableTray1.IsParallel(myCustomCableTray2.CableTray))
			{
				result = new HYCableTray<CableTray>(myCustomCableTray2.CableTray);
			}
			return result;
		}

		public static bool IsMeetVertical(Connector connector1, Connector connector2)
		{
			bool result = false;
			Transform coordinateSystem = connector1.CoordinateSystem;
			Transform coordinateSystem2 = connector2.CoordinateSystem;
			if (coordinateSystem.BasisZ.IsAlmostEqualTo(coordinateSystem2.BasisX, 0.01) || coordinateSystem.BasisZ.IsAlmostEqualTo(-coordinateSystem2.BasisX, 0.01))
			{
				result = true;
			}
			return result;
		}

		private static IList<CableTray> OrderCableTrayPosition(HYCableTray<CableTray> myCustomCableTray, HYCableTray<CableTray> myCustomCableTray1, HYCableTray<CableTray> myCustomCableTray2)
		{
			IList<CableTray> list = new List<CableTray>();
			if (myCustomCableTray.IsCableTrayOnLine(myCustomCableTray1.CableTray) || myCustomCableTray.IsParallel(myCustomCableTray1.CableTray))
			{
				list.Add(myCustomCableTray.CableTray);
				list.Add(myCustomCableTray1.CableTray);
				list.Add(myCustomCableTray2.CableTray);
			}
			else if (myCustomCableTray.IsCableTrayOnLine(myCustomCableTray2.CableTray) || myCustomCableTray.IsParallel(myCustomCableTray2.CableTray))
			{
				list.Add(myCustomCableTray.CableTray);
				list.Add(myCustomCableTray2.CableTray);
				list.Add(myCustomCableTray1.CableTray);
			}
			else
			{
				list.Add(myCustomCableTray1.CableTray);
				list.Add(myCustomCableTray2.CableTray);
				list.Add(myCustomCableTray.CableTray);
			}
			return list;
		}

		public static IList<CableTray> OrderHorizontalCableTrayPosition(HYCableTray<CableTray> myCustomCableTray, HYCableTray<CableTray> myCustomCableTray1, HYCableTray<CableTray> myCustomCableTray2)
		{
			IList<CableTray> list = new List<CableTray>();
			if (myCustomCableTray.IsCableTrayOnLine(myCustomCableTray1.CableTray) || myCustomCableTray.IsParallel(myCustomCableTray1.CableTray))
			{
				list.Add(myCustomCableTray2.CableTray);
				list.Add(myCustomCableTray.CableTray);
				list.Add(myCustomCableTray1.CableTray);
			}
			else if (myCustomCableTray.IsCableTrayOnLine(myCustomCableTray2.CableTray) || myCustomCableTray.IsParallel(myCustomCableTray2.CableTray))
			{
				list.Add(myCustomCableTray1.CableTray);
				list.Add(myCustomCableTray.CableTray);
				list.Add(myCustomCableTray2.CableTray);
			}
			else
			{
				list.Add(myCustomCableTray.CableTray);
				list.Add(myCustomCableTray1.CableTray);
				list.Add(myCustomCableTray2.CableTray);
			}
			return list;
		}

		public static IList<CableTray> OrderCableTrayPosition(IList<CableTray> cableTrays)
		{
			IList<CableTray> list = new List<CableTray>();
			new List<XYZ>();
			HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(cableTrays[0]);
			HYCableTray<CableTray> hycableTray2 = new HYCableTray<CableTray>(cableTrays[1]);
			HYCableTray<CableTray> hycableTray3 = new HYCableTray<CableTray>(cableTrays[2]);
			if (3 == cableTrays.Count)
			{
				if (hycableTray.IsSamePlan(hycableTray2.CableTray) && hycableTray.IsSamePlan(hycableTray3.CableTray))
				{
					list = Common.OrderHorizontalCableTrayPosition(hycableTray, hycableTray2, hycableTray3);
				}
				else if (hycableTray.IsCableTrayOnLine(hycableTray2.CableTray) || hycableTray.IsParallel(hycableTray2.CableTray) || hycableTray.IsSamePlan(hycableTray2.CableTray))
				{
					list.Add(hycableTray3.CableTray);
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray2.CableTray);
				}
				else if (hycableTray.IsCableTrayOnLine(hycableTray3.CableTray) || hycableTray.IsParallel(hycableTray3.CableTray) || hycableTray.IsSamePlan(hycableTray3.CableTray))
				{
					list.Add(hycableTray2.CableTray);
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray3.CableTray);
				}
				else
				{
					list = cableTrays;
				}
			}
			else if (4 == cableTrays.Count)
			{
				HYCableTray<CableTray> hycableTray4 = new HYCableTray<CableTray>(cableTrays[3]);
				if (hycableTray.IsZAxis() && hycableTray2.IsXYPlan() && hycableTray2.IsSamePlan(hycableTray3.CableTray) && hycableTray2.IsSamePlan(hycableTray4.CableTray))
				{
					list = Common.OrderCableTrayPosition(hycableTray2, hycableTray3, hycableTray4);
					list.Add(hycableTray.CableTray);
				}
				else if (hycableTray2.IsZAxis() && hycableTray.IsXYPlan() && hycableTray.IsSamePlan(hycableTray3.CableTray) && hycableTray.IsSamePlan(hycableTray4.CableTray))
				{
					list = Common.OrderCableTrayPosition(hycableTray, hycableTray3, hycableTray4);
					list.Add(hycableTray2.CableTray);
				}
				else if (hycableTray3.IsZAxis() && hycableTray.IsXYPlan() && hycableTray.IsSamePlan(hycableTray2.CableTray) && hycableTray.IsSamePlan(hycableTray4.CableTray))
				{
					list = Common.OrderCableTrayPosition(hycableTray, hycableTray2, hycableTray4);
					list.Add(hycableTray3.CableTray);
				}
				else if (hycableTray4.IsZAxis() && hycableTray.IsXYPlan() && hycableTray.IsSamePlan(hycableTray2.CableTray) && hycableTray.IsSamePlan(hycableTray3.CableTray))
				{
					list = Common.OrderCableTrayPosition(hycableTray, hycableTray2, hycableTray3);
					list.Add(hycableTray4.CableTray);
				}
				else if (hycableTray.IsCableTrayOnLine(hycableTray2.CableTray) || hycableTray.IsParallel(hycableTray2.CableTray))
				{
					list = cableTrays;
				}
				else if (hycableTray.IsCableTrayOnLine(hycableTray3.CableTray) || hycableTray.IsParallel(hycableTray3.CableTray))
				{
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray3.CableTray);
					list.Add(hycableTray2.CableTray);
					list.Add(hycableTray4.CableTray);
				}
				else if (hycableTray.IsCableTrayOnLine(hycableTray4.CableTray) || hycableTray.IsParallel(hycableTray4.CableTray))
				{
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray4.CableTray);
					list.Add(hycableTray2.CableTray);
					list.Add(hycableTray3.CableTray);
				}
				else
				{
					list = Common.OrderCableTrayPosition(hycableTray2, hycableTray3, hycableTray4);
					list.Add(hycableTray.CableTray);
				}
			}
			return list;
		}

		public static bool IsNoFitting(Document doc, CableTray hyct)
		{
			bool result = false;
			if ((doc.GetElement(hyct.GetTypeId()) as CableTrayType).get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString() == "无配件的电缆桥架")
			{
				result = true;
			}
			return result;
		}

		public static bool IsSameCableTrayType(Document doc, IList<CableTray> cableTrays)
		{
			bool result = true;
			CableTrayType cableTrayType = doc.GetElement(cableTrays[0].GetTypeId()) as CableTrayType;
			string a = cableTrayType.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString();
			string name = cableTrayType.Name;
			for (int i = 1; i < cableTrays.Count; i++)
			{
				string b = (doc.GetElement(cableTrays[i].GetTypeId()) as CableTrayType).get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString();
				string name2 = (doc.GetElement(cableTrays[i].GetTypeId()) as CableTrayType).Name;
				if (a != b || name != name2)
				{
					result = false;
					break;
				}
			}
			return result;
		}

		public static Connector GetOtherConnector(HYCableTray<CableTray> hyct, Connector connector)
		{
			Connector connector2;
			if (!hyct.CustomConnectorStart.Connector.Origin.IsAlmostEqualTo(connector.Origin, 0.01))
			{
				connector2 = hyct.CustomConnectorStart.Connector;
			}
			else
			{
				connector2 = hyct.CustomConnectorEnd.Connector;
			}
			return connector2;
		}

		public static bool IsMeetCreateFitting(HYCableTray<CableTray> ct1, Connector connector1, HYCableTray<CableTray> ct2, Connector connector2)
		{
			bool result = false;
			Connector otherConnector = Common.GetOtherConnector(ct1, connector1);
			Connector otherConnector2 = Common.GetOtherConnector(ct2, connector2);
			XYZ xyz = (otherConnector.Origin - connector1.Origin).Normalize();
			XYZ xyz2 = (otherConnector2.Origin - connector2.Origin).Normalize();
			double num = xyz.AngleTo(xyz2);
			if ((num > Math.PI*.5 || Math.Abs(Math.PI*.5 - num) < 0.001) && num < Math.PI)
			{
				result = true;
			}
			return result;
		}

		public const double Approximate = 0.01;
	}
}
